/* Copyright (c) 2010, David Kreuter. See LICENSE for more information */

#include <stdio.h>
#include <malloc.h>

// Diff with % output
// I use this to measure the accuracy of my saving-routines

int main(int argc, char **argv){
	FILE *f,*g;
	if(argc!=3){
		printf("Usage: %s <file-a> <file-b>\n",argv[0]);
		return 1;
	}
	if(!(f=fopen(argv[1],"r"))){
		printf("Couldn't open '%s'\n",argv[1]);
		return 1;
	}
	if(!(g=fopen(argv[2],"r"))){
		printf("Couldn't open '%s'\n",argv[2]);
		return 1;
	}

	fseek(f,0,SEEK_END);
	fseek(g,0,SEEK_END);
	int len_f=ftell(f);
	int len_g=ftell(g);
	if(len_f!=len_g){
		printf("Filesizes don't match '%s' %d (0x%x) '%s' %d (0x%x)\n",
			argv[1],len_f,len_f,argv[2],len_g,len_g);
		return 1;
	}
	rewind(f);
	rewind(g);
	char *buf_f=malloc(len_f);
	char *buf_g=malloc(len_g);
	fread(buf_f,1,len_f,f);
	fread(buf_g,1,len_g,g);

	int same=0;
	int first_diff=len_f;
	while(len_f--){
		if(buf_f[len_f]==buf_g[len_f])same++;
		else first_diff=len_f;
	}

	free(buf_f);
	free(buf_g);
	printf("%10d / %10d # %8x / %8x # %5.1f%% # First difference at %10d %8x\n",
		same,len_g,same,len_g,100*same/(float)len_g,first_diff,first_diff);
}